#include "GradDescent.h"


GradDescent::GradDescent(Func *anFunc): func(anFunc){}


Vector2D GradDescent::gradient(Vector2D v){

	Float h = 0.0001;

	Vector2D xVector(v.getM_X()+h, v.getM_Y());
	Vector2D yVector(v.getM_X(), v.getM_Y()+h);

	// nicht vergessen, dass eine Methode eines Zeigers 
	// (*pointer).funktion oder pointer->funktion aufgerufen wird
	Float newX = (func->f(xVector) - func->f(v)) / h;
	Float newY = (func->f(yVector) - func->f(v)) / h;

	Vector2D newVector2D(newX, newY);

	return newVector2D;
}


Vector2D GradDescent::minimize(Vector2D v, Float m_Step){

	Vector2D newVector(v.getM_X(), v.getM_Y());

	for(int i = 0; i<1000; i++){

		newVector = gradient(newVector) * (-m_Step);
	}

	return newVector;
}


Vector2D GradDescent::maximize(Vector2D v, Float m_Step){

	return minimize(v, (-m_Step));
}